# coding:utf-8

# import time
# timeStamp = 1341244800
# timeArray = time.localtime(timeStamp)
# otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
# print otherStyleTime
import time
from yanzhengma import *
import threading
# import Queue
from threading import Lock
import os
import multiprocessing
from multiprocessing import Queue
lock = Lock()


def justice(te_img_ori, path, num_exp, q):
    # lock.acquire()
    te_img_com = te_img_ori.convert('RGB')
    similar_sum_totals = 0
    similar_sum_dics = {}
    similar_name = ''

    for pic_name in os.listdir(path + r'\%s' % num_exp):
        exp_img = Image.open(path + r'\%s\%s' % (num_exp, pic_name))
        similar_sum = cal_cos_by_img(te_img_com, exp_img)

        if similar_sum > similar_sum_totals:
            similar_sum_totals = similar_sum
            similar_sum_dics[num_exp] = similar_sum_totals
            similar_name = pic_name

    print num_exp, similar_name
    q.put(similar_sum_dics)

# def justice(te_img_ori, path, num_exp, q):
#     # lock.acquire()
#     te_img_com = te_img_ori.convert('RGB')
#     similar_sum_totals = 0
#     similar_sum_dics = {}
#     for pic_name in os.listdir(path + r'\%s' % num_exp):
#         exp_img = Image.open(path + r'\%s\%s' % (num_exp, pic_name))
#         similar_sum = cal_cos_by_img(te_img_com, exp_img)
#
#         if similar_sum > similar_sum_totals:
#             similar_sum_totals = similar_sum
#             similar_sum_dics[num_exp] = similar_sum_totals
#     q.put(similar_sum_dics)

    # print similar_sum_dics
    # lock.release()


def cal_num(tr_imgs):
    similar_sum_dics = {}
    q = Queue()
    ori_path = r'C:\Users\Administrator\Desktop\yzm\example'
    # tr_img = Image.open(r'C:\Users\Administrator\Desktop\yzm\cut\169_4.gif')
    threads = []
    # hehe = time.time()
    for num_exm in os.listdir(ori_path):
        # t = threading.Thread(target=justice, args=(tr_img, ori_path, num_exm))
        t = multiprocessing.Process(target=justice, args=(tr_imgs, ori_path, num_exm, q))
        threads.append(t)

    for thread in threads:
        thread.start()
    for thread in threads:
        # if thread.isAlive():
        thread.join()

    # print q.get()

    while not q.empty():
        # print q.get()
        similar_sum_dics.update(q.get())
    return similar_sum_dics


def cal_method(tr_imgs):
    similar_sum_dics = {}
    q = Queue()
    ori_path = r'C:\Users\Administrator\Desktop\yzm\method'
    # tr_img = Image.open(r'C:\Users\Administrator\Desktop\yzm\cut\169_4.gif')
    threads = []
    # hehe = time.time()
    for num_exm in os.listdir(ori_path):
        # t = threading.Thread(target=justice, args=(tr_img, ori_path, num_exm))
        t = multiprocessing.Process(target=justice, args=(tr_imgs, ori_path, num_exm, q))
        threads.append(t)

    for thread in threads:
        thread.start()
    for thread in threads:
        # if thread.isAlive():
        thread.join()

    # print q.get()

    while not q.empty():
        # print q.get()
        similar_sum_dics.update(q.get())
    return similar_sum_dics



# 将列表内数字相加
def list_add(mylist):
    haha = 0
    for i in mylist:
        if isinstance(i, int):
            haha += i
    return haha


# 得到字典key的最大值
def dic_max_key(mydict):
    dic_max = 0
    value_max = 0
    for key in mydict.keys():
        if mydict.get(key) > value_max:
            value_max = mydict.get(key)
            dic_max = key
    return dic_max

if __name__ == '__main__':

    img2 = hunhe(Image.open(r'C:\Users\Administrator\Desktop\validateCode.jpg'))
    # img2 = hunhe(Image.open(r'C:\Users\Administrator\Desktop\yzm\download_chapcha\79.gif'))
    sentence_list = []
    # img3 = img2.convert('RGB')
    new_img_ob = Img_queue(img2).cut_list
    print 'ok'
    # for tr_img_ori in new_img_ob:

    # tr_img = cut_one(new_img_ob[3])
    # tr_img.show()
    # q = Queue()
    # q = Queue.Queue()

    cut_one(new_img_ob[0]).show()
    cut_one(new_img_ob[1]).show()
    cut_one(new_img_ob[3]).show()

    cap1 = cal_num(cut_one(new_img_ob[0]))
    cap2 = cal_method(cut_one(new_img_ob[1]))
    cap3 = cal_num(cut_one(new_img_ob[3]))

    print dic_max_key(cap1), cap1
    print dic_max_key(cap2), cap2
    print dic_max_key(cap3), cap3

    # ori_path = r'C:\Users\Administrator\Desktop\yzm\example'
    # # tr_img = Image.open(r'C:\Users\Administrator\Desktop\yzm\cut\169_4.gif')
    # threads = []
    # # hehe = time.time()
    # for num_exm in os.listdir(ori_path):
    #     # t = threading.Thread(target=justice, args=(tr_img, ori_path, num_exm))
    #     t = multiprocessing.Process(target=justice, args=(tr_img, ori_path, num_exm, q))
    #     threads.append(t)
    #
    # for thread in threads:
    #     thread.start()
    # for thread in threads:
    #     # if thread.isAlive():
    #     thread.join()
    #
    # # print q.get()
    #
    # while not q.empty():
    #     # print q.get()
    #     similar_sum_dic.update(q.get())

    # print similar_sum_dic
    # sentence_list.append(dic_max_key(similar_sum_dic))
    # if list_add(sentence_list) >= 200:
    #     print sentence_list
        # break
    # for num_exm in os.listdir(ori_path):

    #     justice(tr_img, ori_path, num_exm)
    #     print time.time() - hehe

    # for i in files:
    #     t = threading.Thread(target=player,args=(list[i],))
    #     threads.append(t)
